package com.zhaodui.pos.service.impl;
import com.zhaodui.pos.service.PosXjtaskServiceI;
import org.jeecgframework.core.common.service.impl.CommonServiceImpl;
import com.zhaodui.pos.entity.PosXjtaskEntity;
import com.zhaodui.pos.page.PosXjtaskPage;
import com.zhaodui.pos.entity.PosXjfjEntity;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;
import org.jeecgframework.core.common.exception.BusinessException;
import org.jeecgframework.core.common.service.impl.CommonServiceImpl;
import org.jeecgframework.core.util.MyBeanUtils;
import org.jeecgframework.core.util.StringUtil;
import org.jeecgframework.core.util.oConvertUtils;
import org.jeecgframework.core.util.ApplicationContextUtil;
import org.jeecgframework.core.util.MyClassLoader;
import org.jeecgframework.web.cgform.enhance.CgformEnhanceJavaInter;
import java.util.ArrayList;
import java.util.UUID;
import java.io.Serializable;

import java.util.Map;
import java.util.HashMap;
import org.jeecgframework.minidao.util.FreemarkerParseFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.jeecgframework.core.util.ResourceUtil;

@Service("posXjtaskService")
@Transactional
public class PosXjtaskServiceImpl extends CommonServiceImpl implements PosXjtaskServiceI {

	@Autowired
	private NamedParameterJdbcTemplate namedParameterJdbcTemplate;
	
 	public void delete(PosXjtaskEntity entity) throws Exception{
 		super.delete(entity);
 	}
	
	public void addMain(PosXjtaskPage posXjtaskPage) throws Exception {
		PosXjtaskEntity posXjtask = new PosXjtaskEntity();
		MyBeanUtils.copyBeanNotNull2Bean(posXjtaskPage, posXjtask);
		//保存主信息
		this.save(posXjtask);
		/**保存-附件*/
		List<PosXjfjEntity> posXjfjList = posXjtaskPage.getPosXjfjList();
		for(PosXjfjEntity posXjfj:posXjfjList){
			//外键设置
			posXjfj.setPosXjid(posXjtask.getId());
			this.save(posXjfj);
		}
	}

	
	public void updateMain(PosXjtaskPage posXjtaskPage) throws Exception{
		PosXjtaskEntity posXjtask = new PosXjtaskEntity();
		//保存主表信息
		if(StringUtil.isNotEmpty(posXjtaskPage.getId())){
			posXjtask = findUniqueByProperty(PosXjtaskEntity.class, "id", posXjtaskPage.getId());
			MyBeanUtils.copyBeanNotNull2Bean(posXjtaskPage, posXjtask);
			this.saveOrUpdate(posXjtask);
		}else{
			this.saveOrUpdate(posXjtask);
		}
		//===================================================================================
		//获取参数
		Object id0 = posXjtask.getId();
		//===================================================================================
		//1.附件数据库的数据
	    String hql0 = "from PosXjfjEntity where 1 = 1 AND posXjid = ? ";
	    List<PosXjfjEntity> posXjfjOldList = this.findHql(hql0,id0);
		//2.附件新的数据
		List<PosXjfjEntity> posXjfjList = posXjtaskPage.getPosXjfjList();
	    //3.筛选更新明细数据-附件
		if(posXjfjList!=null &&posXjfjList.size()>0){
			for(PosXjfjEntity oldE:posXjfjOldList){
				boolean isUpdate = false;
				for(PosXjfjEntity sendE:posXjfjList){
					//需要更新的明细数据-附件
					if(oldE.getId().equals(sendE.getId())){
		    			try {
							MyBeanUtils.copyBeanNotNull2Bean(sendE,oldE);
							this.saveOrUpdate(oldE);
						} catch (Exception e) {
							e.printStackTrace();
							throw new BusinessException(e.getMessage());
						}
						isUpdate= true;
		    			break;
		    		}
		    	}
	    		if(!isUpdate){
		    		//如果数据库存在的明细，前台没有传递过来则是删除-附件
		    		super.delete(oldE);
	    		}
	    		
			}
			//4.持久化新增的数据-附件
			for(PosXjfjEntity posXjfj:posXjfjList){
				if(oConvertUtils.isEmpty(posXjfj.getId())){
					//外键设置
					posXjfj.setPosXjid(posXjtask.getId());
					this.save(posXjfj);
				}
			}
		}
	}

	public void delMain(PosXjtaskEntity posXjtask) throws Exception{
		//删除主表信息
		this.delete(posXjtask);
		//===================================================================================
		//获取参数
		Object id0 = posXjtask.getId();
		//===================================================================================
		//删除-附件
	    String hql0 = "from PosXjfjEntity where 1 = 1 AND posXjid = ? ";
	    List<PosXjfjEntity> posXjfjOldList = this.findHql(hql0,id0);
		this.deleteAllEntitie(posXjfjOldList);
	}
 	
}